from django.db import models
from app.models import User


# 动态
class Moments(models.Model):
    # 发布动态的人
    userId = models.ForeignKey(User, on_delete=models.CASCADE, db_column='发布动态的人', verbose_name='发布动态的人')

    # 动态内容
    content = models.CharField(max_length=1000, db_column='动态的内容', verbose_name='动态的内容')

    # 是否公开
    ispublic = models.BooleanField(default=1, db_column='是否公开', verbose_name='是否公开')

    # 发布动态的日期
    date = models.DateTimeField(auto_now=True, db_column='发布时间', verbose_name='发布时间')

    # 图片1
    img1 = models.ImageField(upload_to='imgs/playingplanet/%Y%m%d', db_column='图片1')
    # 如果保存再media目录下，写路径时从media的子目录开始写，在写url时添加上media，这样才能保证文件下载到media目录下

    # 图片2
    img2 = models.ImageField(upload_to='imgs/playingplanet/%Y%m%d', db_column='图片2')

    # 图片3
    img3 = models.ImageField(upload_to='imgs/playingplanet/%Y%m%d', db_column='图片3')

    # 图片4
    img4 = models.ImageField(upload_to='imgs/playingplanet/%Y%m%d', db_column='图片4')

    # 图片5
    img5 = models.ImageField(upload_to='imgs/playingplanet/%Y%m%d', db_column='图片5')

    # 图片6
    img6 = models.ImageField(upload_to='imgs/playingplanet/%Y%m%d', db_column='图片6')

    # 图片7
    img7 = models.ImageField(upload_to='imgs/playingplanet/%Y%m%d', db_column='图片7')

    # 图片8
    img8 = models.ImageField(upload_to='imgs/playingplanet/%Y%m%d', db_column='图片8')

    # 图片9
    img9 = models.ImageField(upload_to='imgs/playingplanet/%Y%m%d', db_column='图片9')

    # 视频
    video = models.ImageField(upload_to='video/playingplanet/%Y%m%d', db_column='视频', verbose_name='视频')

    # 是否显示
    isShow = models.BooleanField(default=1, db_column='是否显示', verbose_name='是否显示')

    # 点赞量
    callNum = models.IntegerField(default=0, db_column='点赞量', verbose_name='点赞量')

    class Meta:
        db_table = '动态'
        verbose_name = verbose_name_plural = db_table
        ordering = ['date']

    @classmethod
    def save_obj(cls, userId, content, public, imgs, video):
        try:
            moment = Moments()
            moment.userId_id = userId
            moment.content = content
            moment.ispublic = public
            moment.video = video
            moment.img1 = imgs[0]
            moment.img2 = imgs[1]
            moment.img3 = imgs[2]
            moment.img4 = imgs[3]
            moment.img5 = imgs[4]
            moment.img6 = imgs[5]
            moment.img7 = imgs[6]
            moment.img8 = imgs[7]
            moment.img9 = imgs[8]
            moment.save()
            return True
        except:
            return False


# 评论表
class JudgeForMoment(models.Model):
    # 评论人
    judger = models.ForeignKey(User, on_delete=models.CASCADE, db_column='评论人', verbose_name='评论人')

    # 评论的动态
    moment = models.ForeignKey(Moments, on_delete=models.CASCADE, db_column='被评论的动态', verbose_name='被评论的动态')

    # 评论的内容
    content = models.CharField(max_length=200, db_column='评论的内容', verbose_name='评论的内容')

    # 评论的日期
    date = models.DateTimeField(auto_now=True, db_column='评论日期', verbose_name='评论日期')

    class Meta:
        db_table = '动态评论'
        verbose_name = verbose_name_plural = db_table
        ordering = ['-date']
